package com.lhkj.ct.admin.modules.questionnaire.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.constants.Constants;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.meta.modules.gauge.model.dto.RemindDto;
import com.lhkj.ct.meta.modules.gauge.service.QrCodeService;
import com.lhkj.ct.meta.modules.gauge.service.RemindService;
import com.lhkj.ct.meta.modules.psychology.model.dto.ProcessDto;
import com.lhkj.ct.meta.modules.psychology.model.dto.ScaleSubmitDto;
import com.lhkj.ct.meta.modules.psychology.model.vo.ViewAnswerVo;
import com.lhkj.ct.meta.modules.questionnaire.model.entity.TblQnrAllotRecord;
import com.lhkj.ct.meta.modules.questionnaire.service.QnrAllotRecordService;
import com.lhkj.ct.meta.modules.questionnaire.service.QnrAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 问卷分配记录 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-26
 */
@RestController
@RequestMapping("api/plat/qnr/allot/record")
@Api(tags = {"5.2问卷分配记录"})
@Validated
public class QnrAllotRecordController {

    @Resource
    private QnrAllotRecordService qnrAllotRecordService;

    @Resource
    private QrCodeService qrCodeService;

    @Resource
    private RemindService remindService;

    @Resource
    private QnrAnswerService qnrAnswerService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "列表查询")
    @GetMapping("list")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<IPage<TblQnrAllotRecord>> selectPatUnitPage(TblQnrAllotRecord record, PageQuery pageQuery) {
        return ActionResult.success(qnrAllotRecordService.selectAllotPage(record, pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取问卷，题目，作答进度等信息", notes = "rId:报告id, info:问卷信息, questions:题目, answerJson:答案json, costTime:计时, anonymous:是否匿名")
    @GetMapping("{allotId}/get")
    @Log(title = "问卷作答", actionType = ActionType.QUERY)
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<Map<String, Object>> get(@PathVariable String allotId) {
        return ActionResult.success(qnrAllotRecordService.selectQnrWithQuestionsByAllotId(allotId, false));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查看作答情况")
    @GetMapping("{allotId}/view")
//    //@RequiresPermissions(value = {"patient:unit:all"}, logical = Logical.OR)
    public ActionResult<ViewAnswerVo> view(@PathVariable String allotId) {
        return ActionResult.success(qnrAllotRecordService.viewAnswer(allotId));
    }

    @PostMapping("submit")
    @ApiOperation(value = "作答提交")
    @Log(title = "调查记录作答", actionType = ActionType.INSERT)
    public ActionResult<Void> submit(@RequestBody ScaleSubmitDto scaleSubmitDto) {
        qnrAnswerService.submitQuestionnaire(scaleSubmitDto);
        return ActionResult.success();
    }

    @PostMapping("process")
    @ApiOperation(value = "保存作答进度")
    public ActionResult<Void> saveProcess(@Validated @RequestBody ProcessDto process) {
        return ActionResult.judge(qnrAnswerService.saveProcess(process));
    }

    @Validated
    @PostMapping("delete")
    @ApiOperation(value = "撤销接口", notes = "支持批量撤销")
    @Log(title = "测验记录", actionType = ActionType.DELETE)
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "撤销的编号不能为空") List<String> ids) {
        int count = qnrAllotRecordService.removeByIds(ids);
        return ActionResult.success(String.format("操作成功，共撤销[%s]条记录", count));
    }

    @PostMapping(value = "remind")
    @ApiOperation(value = "提醒接口")
    public ActionResult<Void> remind(@Valid @RequestBody RemindDto entity) {
        remindService.remind(entity);
        return ActionResult.success("提醒成功");
    }

    @GetMapping(value = "{rId}/qrCode")
    @ApiOperation(value = "二维码接口")
    public ActionResult<String> getQrCode(@PathVariable String rId) throws UnsupportedEncodingException {
        Map<String, Object> queryParams = new HashMap<>();
        UriComponentsBuilder redirectBuilder = UriComponentsBuilder.fromUriString(Constants.SERVICE_BASE_URL).path("/wechat/#/my-questionnaire");
        redirectBuilder.queryParam("allotId", rId);
        String url = redirectBuilder.build().toUriString();
        queryParams.put("url", URLEncoder.encode(url, Constants.UTF8));
        return ActionResult.success("操作成功",qrCodeService.generateQrCode("/ct/api/wechat/scan/qrcode", queryParams));
    }
}
